package itea.bignews.dwr;

import itea.bignews.Comment;
import itea.bignews.News;
import itea.bignews.biz.NewsBiz;
import itea.bignews.sys.ClientConfig;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpSession;

/**
 * 
 * @version $Id: NewsDwr.java 50 2009-09-07 13:39:17Z iteahere $
 * @author itea
 * @date 2009-7-22
 */
public class NewsDwr extends Dwr {

	private NewsBiz newsBiz;
	
	private ClientConfig clientConfig;
	
	public SResult userReadNews(int maxNewsNum) {
		HttpSession session = org.directwebremoting.WebContextFactory.get().getSession();
		Set<Long> readingNewsIds = (Set<Long>) session.getAttribute("reading_news_ids");
		if(readingNewsIds == null) {
			readingNewsIds = new HashSet<Long>();
			session.setAttribute("reading_news_ids", readingNewsIds);
		}
		Collection<News> cn = newsBiz.readNews(maxNewsNum, readingNewsIds);
		return new SResult(clientConfig, cn);
	}
	
	public Collection<Comment> getComments(Long newsId, Comment [] newComments, Long [] raisingComments) {
		for(Comment c : newComments)
			this.postComment(c);
		return newsBiz.getComments(newsId, raisingComments);
	}
	
	public String postNews(News news) {
		newsBiz.post(news);
		return null;
	}
	
	public String postComment(Comment comment) {
		newsBiz.postComment(comment);
		return null;
	}
	
	public Integer raiseComment(Long commentId) {
		return newsBiz.raiseComment(commentId);
	}

	public void setNewsBiz(NewsBiz newsBiz) {
		this.newsBiz = newsBiz;
	}

	public void setClientConfig(ClientConfig clientConfig) {
		this.clientConfig = clientConfig;
	}
}
